{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import libraries\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load Excel File\n",
    "filename = 'data/car_financing.xlsx'\n",
    "df = pd.read_excel(filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Filtering Data\n",
    "Filter out the data to only have data `car_type` of 'Toyota Sienna' and `interest_rate` of 0.0702."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### car_type filter\n",
    "Comparison Operator | Meaning\n",
    "--- | --- \n",
    "< | less than\n",
    "<= | less than or equal to\n",
    "> | greater than\n",
    ">= | greater than or equal to\n",
    "== | equal\n",
    "!= | not equal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's first start by looking at the car_type column. \n",
    "df['car_type'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Notice that the filter produces a pandas series of True and False values\n",
    "car_filter = df['car_type']=='Toyota Sienna'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "car_filter.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Approach 1 using square brackets\n",
    "# Filter dataframe to get a DataFrame of only 'Toyota Sienna'\n",
    "df[car_filter].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Approach 2 using loc\n",
    "# Filter dataframe to get a DataFrame of only 'Toyota Sienna'\n",
    "df.loc[car_filter, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Notice that it looks like nothing changed\n",
    "# This is because we didn't update the dataframe after applying the filter\n",
    "df['car_type'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Filter dataframe to get a DataFrame of only 'Toyota Sienna'\n",
    "df = df.loc[car_filter, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['car_type'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### interest_rate Filter\n",
    "Comparison Operator | Meaning\n",
    "--- | --- \n",
    "< | less than\n",
    "<= | less than or equal to\n",
    "> | greater than\n",
    ">= | greater than or equal to\n",
    "== | equal\n",
    "!= | not equal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['interest_rate'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Notice that the filter produces a pandas series of True and False values\n",
    "df['interest_rate']==0.0702"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interest_filter = df['interest_rate']==0.0702"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.loc[interest_filter, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['interest_rate'].value_counts(dropna = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Combining Filters\n",
    "In the previous sections, we created `car_filter` and `interest_filter` and used the `loc` command to filter the data by first applying the `car_filter` and then the `interest_filter`. An more concise way to do it is shown below. \n",
    "\n",
    "Bitwise Logic Operator | Meaning\n",
    "--- | --- \n",
    "& | and\n",
    "\\| | or\n",
    "^ | exclusive or\n",
    "~ | not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc[car_filter & interest_filter, :]"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
